import 'dart:convert';

import 'package:exams_app/gen_a/A.dart';
import 'package:exams_app/net/Address.dart';
import 'package:exams_app/net/DioManager.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:exams_app/widget/hexcolor.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_verification_box/verification_box.dart';
import 'dart:async';
import 'package:get/get.dart';
import 'register_setpassword_page.dart';
class RegisterGetCodePage extends StatefulWidget {

  var phoneStr;

  RegisterGetCodePage(this.phoneStr);

  @override
  _RegisterGetCodePageState createState() => _RegisterGetCodePageState();
}

class _RegisterGetCodePageState extends State<RegisterGetCodePage> with SingleTickerProviderStateMixin {


  Timer _timer;

  int _countdownTime = 0;

  void startCountdownTimer(){
    const oneSec = const Duration(seconds: 1);

    var callback = (timer) => {
      setState(() {
        if(_countdownTime<1){
          _timer.cancel();
        }else{
          _countdownTime = _countdownTime -1;
        }
      })
    };

    _timer = Timer.periodic(oneSec,callback);
  }


  void cancelTimer() {
    if (_timer != null) {
      _timer.cancel();
      _timer = null;
    }
  }

  @override
  void initState() {
    super.initState();

    startCountdownTimer();

    if(_countdownTime== 0){

      setState(() {
        _countdownTime = 59;
      });

      // requestDataSendCode();

    }

  }

  @override
  void dispose() {

    cancelTimer();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: new IconButton(
          icon: Image.asset(A.assets_arrback_icon),
          onPressed: () => {
            Navigator.of(context).pop('刷新')
          },
        ),
      ),
      body: Column(
        crossAxisAlignment:CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(top: 10,left: 20),
            child: Text('获取验证码',style: TextStyle(fontSize: 24,color: HexColor('#333333'),fontWeight: FontWeight.w500),),
          ),
          Container(
            margin: EdgeInsets.only(top: 5,left: 20),
            child: Text('验证码已经发送给到您的手机',style: TextStyle(fontSize: 12,color: HexColor('#999999')),),
          ),
          Container(
            margin: EdgeInsets.only(top: 5,left: 20),
            child: Text('+86 ${widget.phoneStr}',style: TextStyle(fontSize: 12,color: HexColor('#999999')),),
          ),

          Container(
            child: Row(
              mainAxisAlignment:MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  margin: EdgeInsets.only(top: 50,left: 25),
                  child: Text('6位数验证码',style: TextStyle(fontSize: 12,color: HexColor('#999999')),),
                ),
                Container(
                  margin: EdgeInsets.only(top: 50,left: 25,right: 25),
                  child: GestureDetector(
                    child: Text(
                      _countdownTime > 0?'重新发送(${_countdownTime})':'获取验证码',
                      style: TextStyle(color:Theme.of(context).accentColor,fontSize: 12,),
                    ),
                    onTap: (){

                      requestDataSendCode(widget.phoneStr);

                      startCountdownTimer();
                      if(_countdownTime== 0){
                        setState(() {
                          _countdownTime = 59;
                        });

                      }

                    },
                  ),

                )
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(left: 20,right: 20,top: 10),
            height: 45,
            child: VerificationBox(
              onSubmitted: (value){
                print('${value}');
                Get.to(RegisterSetPasswordPage(widget.phoneStr,value));
              },
              focusBorderColor: Theme.of(context).accentColor,
              showCursor: true,
              textStyle: TextStyle(fontSize: 28,color: HexColor('#242A36')),
              borderColor: HexColor('#D2D6DC'),
              borderWidth: 1,
            ),
          )
        ],
      ),
    );
  }
  Future requestDataSendCode(var tf)async{
    var params = {
      'mobile':tf,
      'name':'登录确认验证码',
      'type':'0',
      'n':'6',
    };
    String result = await DioUtils(url: await PersistentStorage().getStorage('urlStr')).post(Address.sendCode,data:params);

    return jsonDecode(result);

  }
}
